package com.yuanqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanqi.pojo.PurchaseReturnOrderItem;
import com.yuanqi.service.PurchaseReturnOrderItemService;
import com.yuanqi.mapper.PurchaseReturnOrderItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 退货订单明细服务实现类
 */
@Service
public class PurchaseReturnOrderItemServiceImpl extends ServiceImpl<PurchaseReturnOrderItemMapper, PurchaseReturnOrderItem>
    implements PurchaseReturnOrderItemService {

    @Override
    public List<PurchaseReturnOrderItem> getItemsByReturnOrderId(Long returnOrderId) {
        LambdaQueryWrapper<PurchaseReturnOrderItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PurchaseReturnOrderItem::getReturnOrderId, returnOrderId);
        wrapper.orderByAsc(PurchaseReturnOrderItem::getLineNo);
        return list(wrapper);
    }

    @Override
    @Transactional
    public boolean saveBatchItems(List<PurchaseReturnOrderItem> items) {
        if (items == null || items.isEmpty()) {
            return true;
        }
        return saveBatch(items);
    }

    @Override
    @Transactional
    public boolean deleteByReturnOrderId(Long returnOrderId) {
        LambdaQueryWrapper<PurchaseReturnOrderItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PurchaseReturnOrderItem::getReturnOrderId, returnOrderId);
        return remove(wrapper);
    }
}
